package com.team9.fitness.controller;

import com.team9.fitness.service.CommentLikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 评论点赞控制器
 * 
 * 提供评论点赞相关的REST API：
 * - 点赞评论
 * - 取消点赞评论
 * - 切换点赞状态
 * - 查询点赞状态和数量
 */
@Slf4j
@RestController
@RequestMapping("/api/comment-like")
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
public class CommentLikeController {

    @Autowired
    private CommentLikeService commentLikeService;

    //
    // /**
    // * 点赞评论
    // */
    // @PostMapping("/like")
    // public ResponseEntity<Map<String, Object>> likeComment(@RequestParam Long
    // userId, @RequestParam Long commentId) {
    // Map<String, Object> response = new HashMap<>();
    //
    // try {
    // log.info("用户点赞评论: userId={}, commentId={}", userId, commentId);
    //
    // boolean success = commentLikeService.likeComment(userId, commentId);
    //
    // if (success) {
    // response.put("success", true);
    // response.put("message", "点赞成功");
    // response.put("likeCount", commentLikeService.getCommentLikeCount(commentId));
    // } else {
    // response.put("success", false);
    // response.put("message", "点赞失败，可能已经点赞过");
    // }
    //
    // return ResponseEntity.ok(response);
    // } catch (Exception e) {
    // log.error("点赞评论异常", e);
    // response.put("success", false);
    // response.put("message", "点赞失败：" + e.getMessage());
    // return ResponseEntity.ok(response);
    // }
    // }
    //
    // /**
    // * 取消点赞评论
    // */
    // @PostMapping("/unlike")
    // public ResponseEntity<Map<String, Object>> unlikeComment(@RequestParam Long
    // userId, @RequestParam Long commentId) {
    // Map<String, Object> response = new HashMap<>();
    //
    // try {
    // log.info("用户取消点赞评论: userId={}, commentId={}", userId, commentId);
    //
    // boolean success = commentLikeService.unlikeComment(userId, commentId);
    //
    // if (success) {
    // response.put("success", true);
    // response.put("message", "取消点赞成功");
    // response.put("likeCount", commentLikeService.getCommentLikeCount(commentId));
    // } else {
    // response.put("success", false);
    // response.put("message", "取消点赞失败，可能未点赞过");
    // }
    //
    // return ResponseEntity.ok(response);
    // } catch (Exception e) {
    // log.error("取消点赞评论异常", e);
    // response.put("success", false);
    // response.put("message", "取消点赞失败：" + e.getMessage());
    // return ResponseEntity.ok(response);
    // }
    // }

    /**
     * 切换评论点赞状态
     */
    @PostMapping("/toggle")
    public ResponseEntity<Map<String, Object>> toggleCommentLike(@RequestParam Long userId,
            @RequestParam Long commentId) {
        Map<String, Object> response = new HashMap<>();

        try {
            log.info("用户切换评论点赞状态: userId={}, commentId={}", userId, commentId);

            boolean success = commentLikeService.toggleCommentLike(userId, commentId);

            if (success) {
                Boolean isLiked = commentLikeService.getUserLikeStatus(userId, commentId);
                response.put("success", true);
                response.put("message", isLiked ? "点赞成功" : "取消点赞成功");
                response.put("isLiked", isLiked);
                response.put("likeCount", commentLikeService.getCommentLikeCount(commentId));
            } else {
                response.put("success", false);
                response.put("message", "操作失败");
            }

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("切换评论点赞状态异常", e);
            response.put("success", false);
            response.put("message", "操作失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 获取用户点赞状态
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getLikeStatus(@RequestParam Long userId, @RequestParam Long commentId) {
        Map<String, Object> response = new HashMap<>();

        try {
            Boolean isLiked = commentLikeService.getUserLikeStatus(userId, commentId);
            int likeCount = commentLikeService.getCommentLikeCount(commentId);

            response.put("success", true);
            response.put("isLiked", isLiked);
            response.put("likeCount", likeCount);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取点赞状态异常", e);
            response.put("success", false);
            response.put("message", "获取状态失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 获取评论点赞数量
     */
    @GetMapping("/count/{commentId}")
    public ResponseEntity<Map<String, Object>> getLikeCount(@PathVariable Long commentId) {
        Map<String, Object> response = new HashMap<>();

        try {
            int likeCount = commentLikeService.getCommentLikeCount(commentId);

            response.put("success", true);
            response.put("likeCount", likeCount);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取点赞数量异常", e);
            response.put("success", false);
            response.put("message", "获取数量失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
}
